var tree;
var setting = {
	data: {
		simpleData: {
			enable: true
		}
	},
	callback: {
		onClick: onClick
	}
};

function onClick(event, treeId, treeNode, clickFlag) {
	list(1,treeNode.id);
}		


function treeInit(treeNode)
{
	var url = getAction()+"?cmd=goTree";
	$.ajax({
		type: "POST",
		url: url,
		dataType: "json",
		success: function(returnedData){
			$.fn.zTree.init($("#tree"), setting, returnedData);
		}
	});
}


	
function selectNode()
{	var id = tree.getSelectedItemId();
    tree.setItemColor(tree.getSelectedItemId(),"#000000","#f98024");
   
	$$("po.parentId").value=id;
	list(1,"po.parentId="+id);
}

/**
 * 异步加载列表数据
 */
function list(pageNo,pid)
{
	var url = getAction()+"?cmd=data&"+getForm().formSerialize();
	if(pid ==0 || pid){
		var pos = url.indexOf("po.parentId");
		if(pos!= -1){
			var preStr = url.substr(0,pos);
			var nextStr = url.substr(pos+13);
			url =preStr+nextStr;
		}
		url +="&po.parentId="+pid
	}
	$.ajax({
		type: "POST",
		url: url,
		success: function(returnedData){
			$("#dataList").get(0).innerHTML=returnedData;
		}
	});
}

function getAction(){
	return "modules.action";
}

function getForm(){
	return $("#modulesForm");
}

function getDomForm(){
	return $$("modulesForm");
}
/*
 * 跳转到添加页面
 */
function goAdd(){
	var pid = document.getElementById("po.parentId").value;
	var retValue = window.showModalDialog(getAction()+"?cmd=goAdd&po.parentId="+pid,window,"menubar=no;toolbar=no");
}

/*
 * 添加操作，然后返回list页面
 */
function add(){
	if(!validateForm()) return;
	var url = getAction()+"?"+getForm().formSerialize();
	saveOrUpdate(url);
}


/**
 * 添加后返回添加页面继续添加一个记录
 */
function addNext()
{
	if(!validateForm())	return;
	var url = getAction()+"?"+getForm().formSerialize();
	saveOrUpdate(url,"addNext");

}

/**
 * 更新一条记录
 */
function update()
{
	if(!validateForm()) return;
	var url = getAction()+"?"+getForm().formSerialize();
	saveOrUpdate(url);
}



function saveOrUpdate(url,type){
	$.ajax({
		type: "POST",
		url: url,
		dataType:"html",
		success: function(returnedData){
			returnedData = $.evalJSON(returnedData.trim());
			alert(returnedData.msg);
			if(returnedData.processId=='1'){
				var parentWindow = window.dialogArguments;
				parentWindow.list(1);
			}
			if(type !== 'addNext' )	//未定义
				window.close();
			getForm().get(0).reset();		//重置
		}
	});
	
}


function addNode(pid,id,text,value)
{
	tree.insertNewChild(pid,id,text,0,"books_close.gif","books_open.gif","books_close.gif","aaaa");
}

/**
 * 跳转到更新页面
 */
function goPerms(id)
{
	window.showModalDialog(getAction()+"?cmd=goUpdate&formMap.type=perm&po.id="+id,window,"menubar=no;toolbar=no");
}

/**
 * 跳转到更新页面
 */
function goUpdate(id)
{
	var retValue = window.showModalDialog(getAction()+"?cmd=goUpdate&po.id="+id,window,"menubar=no;toolbar=no");
	if(retValue=='1'){
		list($("pageForm.pageNo").value);
	}
}

function updatePerms(){
	var url = getAction()+"?cmd=update&formMap.type=perm&"+getForm().formSerialize();;
	saveOrUpdate(url);
}
/*
 * 删除选择的记录
 */
function del()
{
	if(isSelect()==0){
		alert("请选择需要删除的记录！");
		return;
	}
	if(confirm("您确定要删除这些数据么？")==false)
		return;
	getForm().ajaxSubmit({
			dataType:"html",
			data:{cmd:'delete'},
			success : function(returnedData, statusText, xhr, $form) {
			returnedData = $.evalJSON(returnedData);
			if(returnedData.msg){
				alert(returnedData.msg);
			}
			if(returnedData.processId=='1'){
				list();
			}
		}
	});	
	
	/*	
	$("modulesForm").request({
		parameters: { cmd:'delete'},
		onSuccess:function(transport){
			var retObj= $.evalJSON(transport);
			alert(retObj.msg);
			if(retObj.processId=='1'){
				if($("po.parentId").value=='0'){
					var ids = document.all("formMap.ids");
					for(var i=0;i<ids.length; ++i){
						if(ids[i].checked){
							tree.deleteItem(ids[i].value);
						}
					}
				}
				list(1);
			}
	}}); */
}




/**
 * 表单域校验
 */ 
function validateForm()
{
	

	var success = true;
	if(!checkBlank($$("po.moduleName"),"模块名称")||!checkCharNumber($$("po.moduleName"),"模块名称")||!MaxLength($$("po.moduleName"),"模块名称",30))
	{
		success = false;
	}
	else if(!checkBlank($$("po.moduleCname"),"中文名称")||!MaxLength($$("po.moduleCname"),"中文名称",30))
	{
		success = false;
	}
	else if(!checkBlank($$("po.moduleUrl"),"URL地址")||!MaxLength($$("po.moduleUrl"),"URL地址",255))
	{
		success = false;
	}
	else if(!checkBlank($$("po.moduleClass"),"模块类")||!MaxLength($$("po.moduleClass"),"模块类",300))
	{
		success = false;
	}
	return success;
}

